layui.extend({
    baseConfig : "/js/pages/baseConfig"
}).use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;

    var users = null;
    var fournisseur = getUrlParamer( "fournisseur");
    //绑定校验
    form.verify({
        fournisseur : function(val){
            if( !val || val == ''){
                return "供应商为必选";
            }
        },
        montantTTC : function(val){
            if( !val || val == ''){
                return "Montant TTC不能为空";
            }
        },
    });

    //加载供应商列表
    function loadCooperative(jobNumber) {
        $.get( "/cooperativeNumber/listForSelect/jobNumber/"+jobNumber ,function( res) {
            var cooperativeNumbers = res.data;
            var sel = $("select[name=fournisseur]");
            for (var i = 0; i < cooperativeNumbers.length; i++) {
                var cooperativeNumberModel = cooperativeNumbers[i];
                if( !cooperativeNumberModel.name){
                    continue;
                }
                sel.append("<option value=\"" + cooperativeNumberModel.name + "\">" + cooperativeNumberModel.name + "</option> ")
            }
            //更新渲染
            form.render("select");
            fournisseur = decodeURIComponent(fournisseur)
            sel.val(fournisseur);
            form.render("select");
        });
    }
    let jobNumber = getUrlParamer( "jobNumber");
    loadCooperative(jobNumber);

    //监听提交
    form.on("submit(submitBtn)",function( data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //提交数据
        $.post("/consumer/editCooperative",{
            id : data.field.id,
            fournisseur : data.field.fournisseur,
            montantTTC : data.field.montantTTC
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                    layer.closeAll( "iframe");
                    //刷新父页面
                    // parent.location.reload();
                    let iframe = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframe);
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });
});
